<template>
  <div class="banners-container">
    <el-table :data="bannerList" style="width: 100%" border>
      <el-table-column
        header-align="center"
        type="index"
        width="80"
        label="序号"
        align="center"
      >
      </el-table-column>
      <el-table-column header-align="center" prop="title" label="标题">
      </el-table-column>
      <el-table-column header-align="center" prop="description" label="描述">
      </el-table-column>
      <el-table-column header-align="center" align="center" label="中图">
        <template slot-scope="scope">
          <el-image
            style="width: 100px; height: 100px"
            :src="scope.row.midImg"
            fit="fill"
            :preview-src-list="[scope.row.midImg]"
          ></el-image>
        </template>
      </el-table-column>
      <el-table-column header-align="center" align="center" label="大图">
        <template slot-scope="scope">
          <el-image
            style="width: 100px; height: 100px"
            :src="scope.row.bigImg"
            fit="fill"
            :preview-src-list="[scope.row.bigImg]"
          ></el-image>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        label="操作"
      >
        <template slot-scope="scope">
          <el-tooltip content="内容编辑" placement="top">
            <el-button
              size="mini"
              type="primary"
              @click="handleEdit(scope.row)"
            >
              编辑
            </el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="70%"
      :close-on-click-modal="false"
    >
      <el-form :model="currentBanner">
        <el-form-item label="标题">
          <el-input
            v-model="currentBanner.title"
            placeholder="请输入标题"
          ></el-input>
        </el-form-item>
        <el-form-item label="描述">
          <el-input
            v-model="currentBanner.description"
            placeholder="请输入描述"
          ></el-input>
        </el-form-item>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="中图">
              <UploadImg v-model="currentBanner.midImg" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="大图">
              <UploadImg v-model="currentBanner.bigImg" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleSubmit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getBannerAPI, updateBannerAPI } from "@/api/banner";
import { setDevImg } from "@/utils";
import UploadImg from "@/components/UploadImg";
export default {
  name: "Banners",
  components: {
    UploadImg,
  },
  data() {
    return {
      currentBanner: {},
      dialogVisible: false,
      bannerList: [],
    };
  },
  created() {
    this.getBannerList();
  },
  methods: {
    async getBannerList() {
      const res = await getBannerAPI();
      if (typeof res !== "string") {
        this.bannerList = res.data.map((t) => ({
          ...t,
          midImg: setDevImg(t.midImg),
          bigImg: setDevImg(t.bigImg),
        }));
      } else {
        const { msg } = JSON.parse(res);
        this.$message.error(msg);
      }
    },
    handleEdit(row) {
      this.currentBanner = { ...row };
      this.dialogVisible = true;
    },
    async handleSubmit() {
      const targetIndex = this.bannerList.findIndex(
        (t) => t.id === this.currentBanner.id
      );
      this.bannerList.splice(targetIndex, 1, this.currentBanner);
      const res = await updateBannerAPI(this.bannerList);
      if (typeof res !== "string") {
        this.$message.success("更新成功");
        this.getBannerList();
        this.dialogVisible = false;
      } else {
        const { msg } = JSON.parse(res);
        this.$message.error(msg);
      }
    },
  },
};
</script>

<style scoped lang="scss"></style>
